package com.techstar.mqttprocesser.service.mqtt.handler;

import com.techstar.mqttprocesser.service.mqtt.config.MqttClientProperties;
import org.eclipse.paho.client.mqttv3.MqttClient;

import java.util.HashMap;
import java.util.Map;

/**
 * @author lilei
 * @create 2025-03-27-2025/3/27
 */
public class MqttServiceRegistry {

    /**
     * 保存客户端连接
     * key: deviceName
     * value: MqttClient
     */
    private static final Map<String, MqttClient> clients = new HashMap<>();

    /**
     * 保存主题配置
     * key: topic
     * value: topic配置
     */
    public static final Map<String, MqttClientProperties.TopicConfig> topicMap = new HashMap<>();

    /**
     * 保存主题和客户端的映射
     * key: topic
     * value: deviceName
     */
    public static final Map<String, String> topicDeviceNameMap = new HashMap<>();

    public static void registerClient(String deviceName, MqttClient client) {
        clients.put(deviceName, client);
    }

    public static MqttClient getClient(String deviceName) {
        return clients.get(deviceName);
    }

    public static void registerTopic(String topic, MqttClientProperties.TopicConfig topicConfig) {
        topicMap.put(topic, topicConfig);
    }

    public static MqttClientProperties.TopicConfig getTopicConfig(String topic) {
        return topicMap.get(topic);
    }

    public static void registerTopicDeviceName(String topic, String deviceName) {
        topicDeviceNameMap.put(topic, deviceName);
    }

    public static String getDeviceNameByTopic(String topic) {
        return topicDeviceNameMap.get(topic);
    }


}
